<template>
    <view>

        <view class="content">
           
                <view  v-for="item,idx in listArr" :key="item.id" :item="item" :data_transit="{ index: idx, item: item }" >
                    <view class="item" @click="chooseOne(item)">
                        <view class="name">
                            <text class="tag full" v-if="item.isDefault">默认</text><text class="big mr5"> {{ item.location }}  {{ item.houseNumber }}</text>
                            <!-- {{ item.deliveryAddress }} {{ item.houseNumber }} -->
                        </view>
                        <view class="concat">
                            {{ item.contacts }} ({{ item.sex ==1 ? '先生' : '女士' }}) <text class="ml10">{{ item.contactsPhone }}</text> <text class="tag  ml10" v-if="item.labelStr">{{item.labelStr  }}</text>
                        </view>

                        <view class="right" @click.stop="goEdit(item )">

                            <text class="iconfont icon-xiugai1"></text>

                        </view>
                    </view>
                </view>

                <view v-if="total == 0">
                    <No-data/>
                </view>
            

        </view>

        <view class="foot">
            <view class="likeBtn" @click="goAdd">
                <text> <text class="big">+</text> 新增地址</text>
            </view>

        </view>
    </view>

</template>
<script>
// const chooseLocation = requirePlugin('chooseLocation');
const app = getApp();
export default {
    data () {
        return {
            mode:'', //是否是选择页面 choose 
            positionInfo: {},
            listArr: [],
            total:0,
            tagArr: [{ id: '1', name: "家" }, { id: '2', name: "公司" }, { id: '3', name: "学校" }],
            beforeRoute:'',//保存上一个页面的路径 
        }
    },
    components: {  },
    onShow () {

        // console.log('看看地址', this.beforeRoute )
        
        //判断是不是修改了 或者新增了地址回来的

        // let addresId =  uni.getStorageSync('addresId')

        // if( addresId ){

        //     uni.setStorageSync('choose-addressId', addresId )

        //     uni.removeStorageSync('addresId');


        //     return
        // }

        app.laterToken(bool => {

            app.logRecord('地址列表')

        })



        this.getData()
    },
    onLoad (option) {

        var pages = getCurrentPages();//页面栈
        var beforePage = pages[pages.length - 2];//上个页面

        // console.log('看看页面' , beforePage )

        this.beforeRoute = beforePage.$page.fullPath

        this.mode = option.mode || undefined
    },
    methods: {
        getData () {

            this.$axios({
                url: "flash/supermarket/user/address/page/list?loading", data: {
                    "pageNum": 1,
                    "pageSize": 10,
                }
            })
                .then(res => {

                    this.listArr = res.data.map(item => {

                        item.labelStr = item.label ? this.tagArr.find(i => i.id == item.label)?.name : ''

                        return item

                    })

                    this.total = res.total

                })
        },

        //去修改
        goEdit (item) {
            this.$goNext('/pages/center/address/edit?id=' + item.id)
        },
        goAdd () {
            this.$goNext('/pages/center/address/edit')
        },
        //删除
        delItem( row ){
            // console.log('删除',item)

            this.$axios({url:'flash/supermarket/user/address/batch/del',data:[ row.item.id]})
                .then( res =>{

                    this.listArr.splice(row.data.index,1)
                })
        },
        //选择这个
        chooseOne( row ){

            //如果是选择地址 缓存并且返回
            if( this.mode  == 'choose'){

                uni.setStorageSync('choose-addressId', row.id )

                // this.$goNext( this.beforeRoute )

                uni.navigateBack()

            }

        }

    },
    beforeDestroy(){
        console.log('99999999999999999毁灭执行9999999999999999999999999')
    }
}
</script>
<style lang="scss" scoped>
.content {
    padding: 4rpx 0rpx 80rpx;

    .item {
        position: relative;
        padding: 26rpx;
        padding-right: 80rpx;
        border-bottom: 1rpx solid #f1f1f1;
        white-space: normal;
        .name {
            font-size: 26rpx;
            line-height: 1.5;

            .big {
                font-size: 32rpx;
            }
        }
        .concat {
            font-size: 26rpx;
            margin-top: 4rpx;
            color: #888;
        }
        .tag {
            display: inline-block;
            padding: 2rpx 10rpx;
            border-radius: 3px;
            line-height: 1.2;
            font-size: 28rpx;
            margin-right: 16rpx;
            border: 1px solid #9672f7;
            color: #9672f7;
            // border-color: #9672f7;
            transform: scale(0.8);
            &.full {
                color: #1287fa;
                background: #e6f7ff;
                border-color: #e6f7ff;
            }
        }
        .right {
            position: absolute;
            right: 0rpx;
            width: 100rpx;
            text-align: center;
            top: 0;
            z-index: 4;
            padding-top: 50rpx;
            color: #999;
            font-size: 40rpx;
        }
    }
}
.foot {
    position: fixed;
    left: 0;
    width: 100%;
    bottom: 0;
    padding: 30rpx 0 0rpx;
    height: 70px;
    background: #fff;
    z-index: 2;
    box-shadow: 0 1px 10px #eaeaea;
    .likeBtn {
        font-size: 40rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #ff2d46;
        .big {
            font-size: 50rpx;
            margin-right: 10rpx;
        }
    }
}
</style>